#include"SystemHead.h"

int main()
{
    int fd_first[2];
    int fd_second[2];
    if(pipe(fd_first)==-1)
    {
        perror("pipe");
    }
    if(pipe(fd_second)==-1)
    {
        perror("pipe");
    }
    pid_t pid;
    pid=fork();
    if(pid < 0)
    {
        perror("fork");
        exit(-1);
    }
    if(pid==0)   //child
    {
        sleep(1);
        while(1)
        {
            char buff[1024]={0};
            read(fd_first[0],buff,sizeof(buff));
            printf("parent said to child : %s\n",buff);

            printf("please input message to parent:");
            
            memset(buff,0,sizeof(buff));
          
            scanf("%[^\n]",buff);
            getchar();
            write(fd_second[1],buff,sizeof(buff));
        }
    }
    else         //parent
    {
        while(1)
        {
            char buff[1024]={0};
            
            printf("parent input message to child :");

            memset(buff,0,sizeof(buff));
           
            scanf("%[^\n]",buff);
            getchar();
            write(fd_first[1],buff,sizeof(buff));

            read(fd_second[0],buff,sizeof(buff));
            printf("child said to parent : %s\n",buff);
        }
    }
    return 0;
}